package com.weibo.basic.alg.alert.client;

import com.weibo.basic.alg.alert.client.api.NotifyApi;
import com.weibo.basic.alg.alert.common.entity.JsonReturn;
import com.weibo.basic.alg.alert.common.entity.SendMessageRequest;
import com.weibo.basic.alg.alert.common.interceptor.LoggingInterceptor;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @author zcauniverse@163.com
 * @version 1.0
 * @since 2019-03-08:14:20
 **/
@Slf4j
public class NotifyClient {


    private NotifyApi notifyApi;

    private NotifyClient(String serverUrl, boolean debug) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        if (debug) {
            okHttpClient = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
        }
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(serverUrl)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
        this.notifyApi = retrofit.create(NotifyApi.class);
    }

    public static NotifyClient getInstance(String serverUrl, boolean enableDebug) {

        return new NotifyClient(serverUrl, enableDebug);
    }

    public static NotifyClient getInstance(String serverUrl) {

        return new NotifyClient(serverUrl, false);
    }

    public boolean sendMessage(String msg) {
        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMessage(msg);
        return doSend(sendMessageRequest, 1);
    }

    private boolean doSend(SendMessageRequest request, int retryTimes) {
        JsonReturn<String> resp = null;
        boolean returnFlag = false;
        for (int i = 0; i < retryTimes; i++) {
            try {
                Response<JsonReturn<String>> response = this.notifyApi.sendMsg(request).execute();
                if (!response.isSuccessful()) {
                    log.error("send msg failed,code is {} ,error is {}", response.code(),
                        response.errorBody().string());
                    continue;
                } else {
                    resp = response.body();
                    if (!(returnFlag = resp.isSuccess())) {
                        log.error("send message failed {}", resp.getMsg());
                    } else {
                        break;
                    }
                }
            } catch (IOException e) {
                log.error("send message failed", e);
            }
        }
        return returnFlag;
    }


    public boolean sendMessage(String msg, int retryTimes) {
        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMessage(msg);
        return doSend(sendMessageRequest, retryTimes);
    }

    public boolean sendMessage(String msg, List<String> users, int retryTimes) {
        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setMessage(msg);
        sendMessageRequest.setReceivers(users);
        return doSend(sendMessageRequest, retryTimes);
    }


}
